package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.device.idea.meeting.Addressbook.model.AddrBookProtocol;
import com.device.idea.meeting.Addressbook.model.AddrBookUserInfo;
import com.device.idea.meeting.CallCtrl.model.CallParam;
import com.device.idea.meeting.CallCtrl.model.CallProtocol;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;

public class CallParameterDialog extends Dialog {
    private TextView confSit;
    private TextView callParameterCancel;
    private TextView callParameterConfirm;
    private RadioGroup lineTypeSelect;
    private RadioGroup callTypeSelect;
    private TextView callBandwith;
    private ImageView bandWidthSelect;
    private BandwidthSelectDialog bandwidthSelectDialog;
    private String callNum;
    private Context mContext;
    private AddrBookUserInfo initContactData;


    public CallParameterDialog(@NonNull Context context, String callNumber) {
        super(context);
        callNum = callNumber;
        mContext = context;
        initView();
        initData();
        initListener();
    }

    public CallParameterDialog(@NonNull Context context, AddrBookUserInfo addrBookUserInfo) {
        super(context);
        initContactData = addrBookUserInfo;
        mContext = context;
        initView();
        initData();
        initListener();
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        super.show();
    }

    private void initView() {
        setContentView(R.layout.dialog_call_parameter);
        confSit = findViewById(R.id.conf_sit);
        callParameterCancel = findViewById(R.id.call_parameter_cancel);
        callParameterConfirm = findViewById(R.id.call_parameter_confirm);
        lineTypeSelect = findViewById(R.id.line_type_select);
        callTypeSelect = findViewById(R.id.call_type_select);
        callBandwith = findViewById(R.id.call_bandwidth);
        bandWidthSelect = findViewById(R.id.call_bandwidth_select);
        bandwidthSelectDialog = new BandwidthSelectDialog(mContext);
    }

    private void initData() {
        if (initContactData != null) {
            confSit.setText(initContactData.getName());
            AddrBookProtocol addrBookProtocol = initContactData.getLineType();
            CallProtocol callProtocol = addrBookProtocol.ordinal() < 2 ? CallProtocol.getType(addrBookProtocol.ordinal())
                    : CallProtocol.AUTO;
            RadioButton matchBtn = (RadioButton) lineTypeSelect.getChildAt(callProtocol.ordinal());
            matchBtn.setChecked(true);
            callBandwith.setText(BandwidthSelectDialog.bandwidthMap.get(initContactData.getLineInfo().getCallRate()));
            bandwidthSelectDialog.setCheckedBandwidth(initContactData.getLineInfo().getCallRate());
        }
        confSit.setText(callNum);
        bandwidthSelectDialog.setCheckedBandwidth(3840);  //初始选中4M带宽
    }

    private void initListener() {
        callParameterCancel.setOnClickListener(v -> dismiss());
        callParameterConfirm.setOnClickListener(v -> handleParameterConfirm());
        bandWidthSelect.setOnClickListener(v -> bandwidthSelectDialog.show());
        bandwidthSelectDialog.bandwidthConfirm.setOnClickListener(v -> handleBandwidthConfirm());
    }

    private void handleBandwidthConfirm() {
        RadioButton checkedBandwidth = bandwidthSelectDialog.findViewById(bandwidthSelectDialog.bandwidthSelect.getCheckedRadioButtonId());
        callBandwith.setText(checkedBandwidth.getText());
        bandwidthSelectDialog.dismiss();
    }

    private void handleParameterConfirm() {
        RadioButton checkedBandwidth = bandwidthSelectDialog.findViewById(bandwidthSelectDialog.bandwidthSelect.getCheckedRadioButtonId());
        RadioButton checkedProtocolBtn = findViewById(lineTypeSelect.getCheckedRadioButtonId());
        RadioButton checkedCallTypeBtn = findViewById(callTypeSelect.getCheckedRadioButtonId());
        CallProtocol protocol = CallProtocol.getType(Integer.parseInt(checkedProtocolBtn.getTag().toString()));
        boolean isVideo = checkedCallTypeBtn.getTag() == "1";
        int callRate = Integer.parseInt(checkedBandwidth.getTag().toString());

        CallParam callParam = new CallParam();
        callParam.setDisplayName(callNum);
        callParam.setCalleeNumber(callNum);
        callParam.setProtocol(protocol);
        callParam.setVideo(isVideo);
        callParam.setRate(callRate);
        ConfSdkPresenter.startCallWithParam(callParam);
    }
}
